* Firm organization with multiple establishments
* Section III.C data preparations 2000-2010 data robustness SE

clear all
set matsize 4000
set more off

capture log close
log using log/15_facts-p_dynMEorg_SE_data.log, replace

use untid betnr jahr persnr beruf w93_3_gen ao_kreis hauptbet layer ///
	if jahr >= 2000 & jahr <= 2010 using data/Panel.dta, clear
desc

********************************************************************************
***	Sample restriction *********************************************************
********************************************************************************

*	Only ever single-establishment firms
egen flg_estjhr = tag(betnr jahr)
bys untid jahr: egen count_est = total(flg_estjhr)
by  untid: egen ever_ebu = min(count_est)
qui keep if ever_ebu == 1
drop ever_ebu

*	Minimum firm size
bys untid jahr: egen empl_unt = count(persnr)
by  untid: egen min_empl = min(empl_unt)
qui keep if min_empl >= 10
drop min_empl

sort betnr jahr layer
egen flg_estjhrlay = tag(betnr jahr layer)
keep if flg_estjhrlay == 1
drop flg_estjhrlay

keep untid jahr betnr count_est empl_unt ao_kreis layer
egen max_jahr = max(jahr), by(untid)

********************************************************************************
***	Layer classification based on KldB1988, managerial organization ************
********************************************************************************

tab layer, missing
//	layer based on KldB1988_LayerCMRHFriedrich.dta; SE firms -- whether layers assigned at est. or firm level does not matter

bys betnr jahr: egen lowest = min(layer)
bys betnr jahr: egen second_lowest = min(layer) if layer > lowest
bys betnr jahr: egen third_lowest = min(layer) if layer > second_lowest
bys betnr jahr: egen highest = min(layer) if layer > third_lowest

gen layer_rank = .
replace layer_rank = 0 if layer == lowest
replace layer_rank = 1 if layer == second_lowest & layer_rank == .
replace layer_rank = 2 if layer == third_lowest & layer_rank == .
replace layer_rank = 3 if layer == highest & layer_rank == .
drop lowest *_lowest highest

//	Number of managerial layers
egen flg_untjhrlay = tag(untid jahr layer_rank)
qui replace flg_untjhrlay = 0 if layer_rank == 0
bys untid jahr: egen count_mgmt_unt = total(flg_untjhrlay)
drop flg_untjhrlay

egen flg_untjhr = tag(untid jahr)
qui keep if flg_untjhr == 1
drop flg_untjhr

********************************************************************************
***	Firm size ******************************************************************
********************************************************************************

merge m:1 untid jahr using data/Amadeus_Aug2018_untid.dta, keepusing(Sales) 
drop if _merge == 2
drop _merge 

********************************************************************************
***	Restrict *******************************************************************
********************************************************************************

keep untid jahr count_est empl_unt Sales count_mgmt_unt max_jahr
duplicates drop

xtset, clear
xtset untid jahr

count

order untid jahr count_est empl_unt max_jahr count_mgmt_unt 

********************************************************************************
***	Future organizational structure ********************************************
********************************************************************************

/*
Cases in period t: only single-establishment firms
0
1
2
3

Cases in period t+1:
0
1
2
3
ME
exit
*/

***	Firm level
tab count_mgmt_unt, m
label variable count_mgmt_unt "\# mgmt. layers, t"
tab count_mgmt_unt if count_est > 1, m

gen f_count_mgmt_unt = F.count_mgmt_unt
label variable f_count_mgmt_unt "\# mgmt. layers, t+1"
tostring f_count_mgmt_unt, replace
replace f_count_mgmt_unt = "" if f_count_mgmt_unt == "."
replace f_count_mgmt_unt = "ME" if F.count_est > 1 & F.count_est != .
replace f_count_mgmt_unt = "Exit" if f_count_mgmt_unt == "" & jahr < 2010
order f_count_mgmt_unt, after(count_mgmt_unt)

drop if jahr == 2010

desc
compress 

save "data/MEorg_2000-2010_p_SE_transition.dta", replace

********************************************************************************
********************************************************************************

log close
